package com.mk.car.dao;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.Daos;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.ioc.loader.json.JsonLoader;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.alibaba.fastjson.JSON;
import com.mk.MyNutTestRunner;
import com.mk.car.entity.Car;
import com.mk.common.dao.CommonService;
import com.mk.common.entity.DE;
import com.mk.common.entity.Person;
import com.mk.common.util.StringUtils;

@RunWith(MyNutTestRunner.class)
@IocBean
public class KTM
{
	@Inject
	Ioc ioc;
	
	@Inject
	Dao dao;
	
	@Inject
	CommonService commonService;
	
	/**
	 * 日志
	 */
	private static Log logger = Logs.getLog(KTM.class);
	
	SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
	
	@Test
	public void test1()
	{
		ioc = new NutIoc(new JsonLoader("ioc/dao.js"));
		dao = ioc.get(Dao.class);
		
		Pager pager = dao.createPager(1, 4);
		List<Car> list = dao.query(Car.class, null, pager);
		
		for(Car c: list)
			System.out.println(c);
	}
	
	@Test
	public void test2()
	{
		DE de = new DE();
		de.setId("12");
		de.setName("之首");
		dao.create(DE.class, true);
		
		dao.insert(de);
		System.out.println(de);
	}
	
	@Test
	public void test3()
	{
		DE d = new DE();
		d.setName("何沐春风");
		d.setAge(2120);
		d.setId("378273iehwqjidnkj");
		dao.insert(d);
		
		System.out.println(d);
	}
	
	@Test
	public void test4()
	{
		Cnd cnd = Cnd.where("1", "=", 1);
		Cnd cnd1 = Cnd.where("1", "=", 1);
		
		System.out.println("....."+cnd.toString().equals(cnd1.toString()));
		System.out.println(cnd.toSql(null)); // WHERE 1=1
		System.out.println(cnd1.toString()); // WHERE 1=1
	}
	
	@Test
	public void test5()
	{
		Cnd cnd = null;
//		cnd = cnd.and("1","=",1); //exception
		System.out.println(cnd);
		
		byte[] res = new byte[0];
		byte[] res1 = new byte[]{};
	}
	
	@Test
	public void test6()
	{
		Condition c = Cnd.wrap("a=b");
		
		System.out.println(c); //a=b
		System.out.println(Cnd.wrap("a=b")); //a=b
	}
	
	@Test
	public void test7()
	{
		System.out.println( "".equals("  ".trim())  );
	}
	
	
	@Test
	public void test8()
	{
		Car car = new Car();
		car.setCarno("沪C·AD1330");
		car.setKilometers(1200.00);
		car.setRemark("测试 新增");
		car.setMaxload(3200.23);
		car.setLoading(0.00);
		car.setServiceDate(new Date());
		car.setStatus(1);
		car.setLocation("00052272200000");
		commonService.insertBasicObj(car);
		
		System.out.println(car);
	}
	
	@Test
	public void test9()
	{
		Person p = (Person) dao.fetch(Person.class, Cnd.where("id","=","5tj2k98g4egrsplq5hde9u5mks"));
		System.out.println("---------------输出结果------");
		System.out.println(p);
	}
	
	@Test
	public void test10() {
		Sql sql = Sqls.create("select car.carno,dic_region.name from car,dic_region " + 
				"where car.location=dic_region.id and dic_region.id =@region");
		sql.params().set("region", "00021030400000");
//		Record record =  (Record) dao.execute(sql);//×××××××
		dao.execute(sql);
		
		System.out.println("sql: ===== "+sql);
		System.out.println("$$$: "+sql.getString());
		
//		System.out.println(record.getString("carno"));
//		System.out.println(record.getString("name"));
	}
	
	@Test
	public void test11()
	{
		Car car = dao.fetch(Car.class, Cnd.where("location","=","00021030400000"));
//		System.out.println(car);
		logger.debug(car);
	}
	
	
	@Test
	public void test12() 
	{
		Sql sql = Sqls.create("select car.* from car,dic_region " + 
				"where car.location=dic_region.id and dic_region.id =@region");
		//参数赋值
		sql.params().set("region", "00021030400000");
		//设置实体
		Entity<Car> entity = dao.getEntity(Car.class);
		sql.setEntity(entity);
		//执行
		dao.execute(sql);
		
		Car car= sql.getObject(Car.class);
		System.out.println("输出: "+car); //null
	}
	
	
	@Test
	public void test13() 
	{
		Sql sql = Sqls.fetchEntity("select car.* from car,dic_region " + 
				"where car.location=dic_region.id and dic_region.id =@region");
		//参数赋值
		sql.params().set("region", "00021030400000");
		//设置实体
		Entity<Car> entity = dao.getEntity(Car.class);
		sql.setEntity(entity);
		//执行
		dao.execute(sql);
		
		Car car= sql.getObject(Car.class);
		System.out.println("输出: "+car); //√√√√√√√
	}
	
	@Test
	public void test13_() {
		Sql sql = Sqls.queryEntity("select car.* from car,dic_region " + 
				"where car.location=dic_region.id and dic_region.id =@region");
		
		//参数赋值
		sql.params().set("region", "00021030400000");
		//设置实体
		Entity<Car> entity = dao.getEntity(Car.class);
		sql.setEntity(entity);
		//执行
		dao.execute(sql);
		
		List<Car> cars = sql.getList(Car.class);
		System.out.println(cars);
		System.out.println("长度: "+cars.size());
	}
	
	
	
	@Test
	public void test14()
	{
		Person p = new Person();
		p.setId("id_2");
		p.setPersonName("随染");
		p.setPersonWeight(null);
		
		System.out.println(JSON.toJSONString(p)); //{"id":"id_2","personName":"随染"}
		System.out.println( JSON.toJSON(p) );
	}
	
	/**
	 * 不满足 查询条件时，集合查询返回空集合，对象查询返回空指针
	 */
	@Test
	public void test15() {
		List<Car> cars = 
				dao.query(Car.class, Cnd.where("carno", "=", "aaaaa"));
		
		System.out.println(cars); // []
		System.out.println(cars.size()); //0
	}
	@Test
	public void test16() {
		Car car = dao.fetch(Car.class, 20000000);
		System.out.println(car);  //null
	}
	

	
	@Test
	public void test18() {
		List<String> list = new ArrayList<>();
		list.add("130102199603070285");
		
//		String[] idCards = (String[]) list.toArray();
		String[] idCards = list.toArray( new String[list.size()] );
		
		System.out.println(idCards.length);
	}
	
	@Test
	public void test19() {
		BigDecimal incomeFamilyTotal = new BigDecimal("0");
		BigDecimal incomeAfterRemit = new BigDecimal("120");
		
		incomeFamilyTotal.add(incomeAfterRemit);
		
		System.out.println(incomeFamilyTotal.toString());
	}
	
	
	@Test
	public void test20() {
		Sql sql = Sqls.fetchString("select carno from car where id=@id ");
		//参数赋值
		sql.params().set("id", "10");
//		//设置实体
//		Entity<String> entity = dao.getEntity(String.class);
//		sql.setEntity(entity);
		//执行
		dao.execute(sql);
		
		String id = sql.getString();
		System.out.println("输出: "+id); 
	}
	
	@Test
	public void test21() {
		Sql sql = Sqls.create("update car set carno=@carno where id = @id");
		sql.params().set("carno", "苏A·MT996");
		sql.params().set("id", "2");
		dao.execute(sql);
	}
	
	
	/**
	 * 自定义SQL进行分页查询，
	 */
	@Test
	public void test22() {
		Sql sql = Sqls.create("SELECT * from car where id < @id");
		sql.params().set("id", 10);
		
		//设置回调
		Entity<Record> entity = dao.getEntity(Record.class);
		sql.setEntity(entity);
		sql.setCallback(Sqls.callback.entities());
		
		//分页条件
		Pager pager = dao.createPager(2, 2);
		sql.setPager(pager);
		
		//查询总数
		long count = Daos.queryCount(dao, sql);
		
		//执行查询
		dao.execute(sql);
		//查询列表
		List<Record> list = sql.getList(Record.class);
		
		for(Record record : list) {
			System.out.println(record);
		}
		System.out.println("总数: "+count);
		System.out.println("列表长度: "+list.size());
		
		//记录转化
		Map<String,Object> resultMap = new HashMap<>(2);
		resultMap.put("data", list);
		resultMap.put("count", list);
		System.out.println(JSON.toJSONString(resultMap));
	}
	
	@Test
	public void test23() {
		String personName = "李廷洲";
//		String personName = "";
//		String idCard = "110101199003071073";
		String idCard = "";
		
		
//		Cnd cnd = Cnd.where("1", "=", 1);
		Cnd cnd = Cnd.NEW();
		cnd.where().setTop(false);
		cnd.and(new Static("qua where 1 = 1 "));
		if(!StringUtils.isEmpty(personName) || !StringUtils.isEmpty(idCard) ) {
			cnd.and(new Static("exists (select 1 from GOMZ_DATA_HAB06 where e.BHAX0001 = BHAX0001 "));
			cnd.andEX("AHAP0016", "=", personName);
			cnd.andEX("AHAP0015", "=", idCard);
			cnd.and(new Static("1 = 1)"));
		}
		
		System.out.println("-------------------   结果    --------------------");
		System.out.println(cnd.toString());
	}
	
	
	@Test
	public void test24() {
		Cnd cnd = Cnd.NEW();
		cnd.where().setTop(false);
		
		
		cnd.and("1", "=", 1);
		
		System.out.println("------------------------    结果     ---------------------");
		System.out.println(cnd.toString()); //(1=1)
		System.out.println();
		System.out.println();
	}
	
	@Test
	public void test25() {
		Cnd cnd = Cnd.NEW();
		cnd.where().setTop(false);
		cnd.and("1", "=", 1);
		cnd.and("p.REGION_ID", "IS", null);
		cnd.orEX("p.REGION_ID", "=", "");
		
		System.out.println(cnd.toString());
	}
	
	@Test
	public void test26() {
		System.out.println(13&17);
	}
	
	@Test
	public void test27() {
		Double applyHours = 0.0;
		applyHours += 1.0;
		
		System.out.println(applyHours);
	}
	
	
}
